package com.codeyang.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 描述:
 * 解决服务调用的token的传递问题
 * 问题:
 * 1.Fegin 远程调用会丢失请求头.
 * 2.客户端之间的调用是没有用户的,此时也需要一个 "临时" token
 *
 * @author CodeYang_Site
 * @version 2021/5/22 20:56
 */
@Component
public class FeignInterceptor implements RequestInterceptor {


    /**
     * 解决token传递丢失问题
     * 1.客户端->服务->服务之间的调用需要copy token
     * 2. 服务之间调用需要一个 "临时66年"的 的token
     *
     * @param template
     */
    @Override
    public void apply(RequestTemplate template) {
//        //1.获得请求
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
//        //2 copy 请求信息
//        //1 获取到浏览器带进来的token就这样去操作
//        String authorization = request.getHeader("Authorization");
//        if (!ObjectUtils.isEmpty(authorization)){
//            Enumeration<String> headerNames = request.getHeaderNames();
//            HashMap<String, String> map = new HashMap<>();
//            while (headerNames.hasMoreElements()) {
//                String headerKey = headerNames.nextElement();
//                String headerValue = request.getHeader(headerKey);
//                map.put(headerKey, headerValue);
//            }
//            if (map.size() > 0) {
//                // 把请求过来的header请求头 原样设置到feign请求头中
//                // 包括token
//                for (Map.Entry<String, String> entry : map.entrySet()) {
//                    template.header(entry.getKey(), entry.getValue());
//                }
//            }
//            return;
//        }
        // 获取到浏览器带进来的token
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (!ObjectUtils.isEmpty(requestAttributes)) {
            HttpServletRequest request = requestAttributes.getRequest();
            String authorization = request.getHeader("Authorization");
            if (StringUtils.hasText(authorization)) {
                // 传递到下一个请求中去
                template.header("Authorization", authorization);
                return;
            }
        }

        //没有token就当是其他有需要远程调用
        // 没有token  但是他有需要远程调用
        //TODO openfeig客户端的token问题!  这里的token第三方申请应该直接申请一个66年的放入
        template.header("Authorization", "bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzY29wZSI6WyJhbGwiXSwiZXhwIjozNzcwMDM5MDE1LCJqdGkiOiJlYjg1NjFlZC1lZTc2LTQwYzYtYjk5MS04NWQwNWVmNWJlNzIiLCJjbGllbnRfaWQiOiJjbGllbnQifQ.U1scFB1Sf-X1pGCwQ6f5H5snlxfy1MiIeTF9zqJL5Gy2sxpY1ptBwEg5AqycGZbK3AJraiEZcl0YfAJ8uGLhPwTzAvRk1FsvnE33EVZ1IgqSlmZPWqhzHycWwXAPr4lAK_8Df7hn3rYdp2XFFjAUJUAjzGMYNVNczli585bSBglqoZeIbUWIcHNpx7KiWMqa2Qqbgwgk0N_rNsBBl0gWszfsxU6bLYlaPoogUZAbAgeHmtMlQ3WPb7yVgKkP2mnW7390rvIDeXg8qAqPN6D6qzJ1dY75mZ7tEiUgAyr411CmV2AL9wicBHu4NLHrfvbCbxdA0mJ9GWSiMrPZI10kYA");



    }


    private Map<String, String> getHeaders(HttpServletRequest request) {

        Map<String, String> map = new LinkedHashMap<>();

        Enumeration<String> enums = request.getHeaderNames();
        while (enums.hasMoreElements()) {
            String key = enums.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }

        return map;
    }
}
